﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QFlow.Utils;
using QFlow.Data;

namespace QFlow.Windows.Forms
{
    public partial class ProductStatus : BaseForm
    {
        public ProductStatus(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
        }

        private void ProductStatus_Activated(object sender, EventArgs e)
        {
            lvExpired.Items.Clear();
            lvLimit.Items.Clear();


            FillExpired();

            FillLimited();
        }

        private void FillExpired()
        {
            int pendingStatusId = Context.OrderStatus.GetPendingStatus().StatusID;
            int closedStatusId = Context.OrderStatus.GetClosedStatus().StatusID;
            int receivedStatus = Context.ItemTypes.GetReceivedType().ItemTypeID;

            var expired = from ex in Context.PurchaseItems
                          where (ex.ExpirationDate - DateTime.Now.Date).Days <= 30
                          && ex.ItemTypeID == receivedStatus
                          && (ex.PurchaseOrder.StatusID == closedStatusId || ex.PurchaseOrder.StatusID == pendingStatusId)

                          orderby ex.ExpirationDate ascending
                          select new
                          {
                              ProductID = ex.ProductID,
                              Product = ex.Product.Name,
                              Date = ex.ExpirationDate,
                              Quantity = (ex.Quantity - ex.Sold - (ex.PurchaseItems.SingleOrDefault() == null ? 0 : ex.PurchaseItems.Single().Quantity)),
                              OrderID = ex.PurchaseOrderID
                          };

            foreach (var item in expired)
            {
                if (item.Quantity > 0)
                {
                    ListViewItem lvItem = new ListViewItem(item.ProductID.ToString());
                    lvItem.SubItems.Add(item.Product);
                    lvItem.SubItems.Add(item.Date.ToString("dd-MM-yyyy"));
                    lvItem.SubItems.Add(item.Quantity.ToString());
                    lvItem.SubItems.Add(item.OrderID.ToString());

                    lvItem.ForeColor = Color.Red;
                    lvItem.Font = new Font("Arial", 10.5f);

                    lvExpired.Items.Add(lvItem);
                }
            }
        }

        private void FillLimited()
        {
            var limited = from product in Context.StockableProducts
                          where product.CurrentQuantity <= product.ReorderPoint

                          select new
                          {
                              ProductId = product.ProductID,
                              Product = product.Product.Name,
                              Limit = product.ReorderPoint,
                              Quantity = product.CurrentQuantity
                          };

            foreach (var item in limited)
            {
                ListViewItem lvItem = new ListViewItem(item.ProductId.ToString());
                lvItem.SubItems.Add(item.Product);
                lvItem.SubItems.Add(item.Limit.ToString());
                lvItem.SubItems.Add(item.Quantity.ToString());

                lvItem.ForeColor = Color.Orange;
                lvItem.Font = new Font("Arial", 10.5f);

                lvLimit.Items.Add(lvItem);
            }
        }
    }
}
